package cn.jiangjiesheng.bootstrap.gateway;

import cn.jiangjiesheng.bootstrap.gateway.auth.AuthFilterProperties;

import lombok.extern.slf4j.Slf4j;
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Primary;

import javax.annotation.PostConstruct;
import java.util.concurrent.TimeUnit;

/**
 * @author dev@jiangjiesheng.cn
 */
@Slf4j
@EnableConfigurationProperties({AuthFilterProperties.class
//        , PaasProperties.class,
//        PaasDevelopProperties.class,
//        LegacyPaasProperties.class
})
@SpringBootApplication(exclude = {
//        ApaasJdbcAutoConfiguration.class,
        IntegrationAutoConfiguration.class,
//        ApaasAutoConfiguration.class
})
@EnableDiscoveryClient
public class GatewayApplication {

    @Value("${spring.cloud.nacos.config.namespace:''}")
    private String nacosNamespace;

    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }

    @PostConstruct
    public void printLog() {
        log.info("\n\n GatewayApplication nacos config 命名空间（注意核对）: {}\n", nacosNamespace);
    }

    @Bean
    public OkHttpClient okHttpClient() {
        return new OkHttpClient.Builder()
                .connectTimeout(30, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS)
                .writeTimeout(30, TimeUnit.SECONDS)
                .retryOnConnectionFailure(true)
                .connectionPool(new ConnectionPool(10, 5L, TimeUnit.MINUTES))
//                .addInterceptor(new OkHttpLogInterceptor())
                .build();
    }
//
//    @Bean
//    public PaasTemplate paasTemplate(PaasProperties paasProperties) {
//        return new PaasTemplate(paasProperties);
//    }
//
//    @Primary
//    @Bean(name = "paasConfig")
//    public LegacyPaasProperties paasConfig(LegacyPaasProperties legacyPaasProperties) {
//        return legacyPaasProperties;
//    }
//
//
//    /**
//     * legacy needed
//     *
//     * @return
//     */
//    @DependsOn("paasConfig")
//    @Bean
//    public PlatformService platformService() {
//        return new PlatformService();
//    }

}
